function initTopSubMenu(){
	$(".topMenu .subMenu").hide();
	$(".subMenu:first").addClass("firstSubMenu");
	$(".subMenu:last").addClass("lastSubMenu");
	$(".menuTitle").bind("mouseenter",function(){
		if($(this).parent().find(".subMenu")){
			$(".topMenu .subMenu").hide();
			$(this).parent().parent().find(".subMenu").show();
			$(".menuTitle").removeClass("hoverBg");
			$(this).addClass("hoverBg");
		}
	});
	$(".topMenu .subMenu ul").bind("mouseleave", function(){
		$(".topMenu .subMenu").hide();
		$(".menuTitle").removeClass("hoverBg");
	});
	$(".topMenu").bind("mouseleave", function(){
		$(".topMenu .subMenu").hide();
		$(".menuTitle").removeClass("hoverBg");
	});
}

$(function () {
    var $backToTopTxt = "To Top", $backToTopEle = $('<div class="toTop img-circle">Top</div>').appendTo($("body"))
        .attr("title", $backToTopTxt).click(function () {
            $("html, body").animate({ scrollTop: 0 }, 120);
        }), $backToTopFun = function () {
            var st = $(document).scrollTop(), winh = $(window).height();
            (st > 300) ? $backToTopEle.show() : $backToTopEle.hide();
            //$(".toTop").css("left",parseInt((document.body.offsetWidth-960)/2)+961);
            //IE6下的定位
            if (!window.XMLHttpRequest) {
                $backToTopEle.css("top", st + winh - 140);
            }
        };
    $(window).bind("scroll", $backToTopFun);
    $(function () { $backToTopFun(); });

    initTopSubMenu();
});

function closeSubscribeDialog() {
    if(window["subscriber-dialog"]) {
        window["subscriber-dialog"].close();
        window["subscriber-dialog"] = null;
    }
}

function openSubscribeDialog() {
    window["subscriber-dialog"] = $.dialog.open("/account/subscribe",
        {
            title: "Subscribe",
            width: "666px",
            height: "298px"
        }, false).lock();
}


jQuery.PikaQajax = function (options) {
    options = options || {};
    var originalSuccess = options.success;
    options.dataType = "json";
    options.headers = options.headers || {};
    options.headers["pickq"] = true;
    options.success = function (result, textStatus, jqxhr) {
        if (result.actionCode == "do") {
            if (originalSuccess && $.isFunction(originalSuccess)) {
                originalSuccess(result.actionData, textStatus, jqxhr);
            }
        } else {
            if (result.action == "login") {
                top.location.href = "/Logout.aspx";
            }
        }
    };
    $.ajax(options);
};

String.isEmail = function (value) {
    return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value);
};

function apply_subscribe() {
    $("#global_subscribe_email_tip").html("");
    $("#global_subscribe_name_tip").html("");
    var valid = true;
    var email = $.trim($("#global_subscribe_email").val());
    if (!email) {
        valid = false;
        $("#global_subscribe_email_tip").html("email is required.");
    }else if (!String.isEmail(email)) {
        valid = false;
        $("#global_subscribe_email_tip").html("error email account.");
    }
    var name = $.trim($("#global_subscribe_name").val());
    if (!name) {
        valid = false;
        $("#global_subscribe_name_tip").html("name is required.");
    }
    if (valid) {
        var phone = $.trim($("#global_subscribe_telephone").val());
        var ajaxOptions = {
            url: "/account/dosubscribe",
            data: { "email": email, "name": name, "phone": phone },
            type: "post",
            success: function (data) {
                if (data.Status) {
                    parent.success_subscribe();
                } else {
                    parent.fail_subscribe();
                }
            }
        };
        $.PikaQajax(ajaxOptions);
    }
}

function success_subscribe() {
    closeSubscribeDialog();
    art.alert("sucscribe success.");
}

function fail_subscribe() {
    closeSubscribeDialog();
    art.alert("sucscribe fail.");
}
